<?php
/**
 * 激活码
 *
 * @author      
 * @version     V1.0.0.1
 * @since        1.0
 */

namespace Dyadmin\Model;
use Think\Model;

class InvitationCodeModel extends Model {
	
	/*
	 * 邀请码校验是否有用过
	 * 
	 */
	public function chk($code){
		
		$w['code'] =$code;
		$res=$this->where($w)->find();
		$return['status']='0';
		if(empty($res)){
			
			$return['msg']='邀请码未找到';
			return $return;
		}
		
		$stats=$res['status'];
		if($stats==1){
			$return['msg']='邀请码已经使用';
			return $return;
		}
		
		$end_time=$res['end_time'];
        $add_time=$res['add_time'];
		$now = time();

        if((int)$now-(int)$add_time<0){
            $return['msg']='邀请码未开始使用'.date('Y-m-d H:i:s',$add_time);
            return $return;
        }

		if((int)$now-(int)$end_time>0){
			$return['msg']='邀请码已经过期'.date('Y-m-d H:i:s',$end_time);
			return $return;
		}
		$return['status']='1';
        $return['msg']='邀请码可使用';
		$return['data']=$res;
		return $return;
		
	}
	
	
	public  function  register($user_id,$code){
		$save['deal_time']=time();
		$save['user_id']=$user_id;
		$save['status']=1;
		$res=$this->where(['code'=>$code])->save($save);
		if($res){
			return true;
		}else {
			return false;
		}
	}
    public  function  addcode($code_num,$prefix,$end_time,$login_name,$code_len)
    {
        if(empty($code_len)){
            $code_len=20;
        }
        if(strlen($code_len)>30){
            $return['status'] = 0;
            $return['error'] = "长度不能超过30位";
            return $return;
        }
        $prefix = strtoupper($prefix);
        $agent_info = $this->checkagent($login_name);
        //var_dump($agent_info);exit;
        if ($agent_info) {
            if ($agent_info['is_agent'] == 1) {
                    if (!empty($prefix) && strlen($prefix) <= 4) {
                        if ($this->checkprefix($prefix)) {
                            if ($code_num != "" && $code_num != 0 && !is_nan($code_num) && $code_num <= 200) {
                                if ($end_time != "") {
                                    $times = explode("~", $end_time);
                                    $codes = $this->getcode($prefix, $code_num, $code_len-strlen($prefix));
                                    for ($i = 0; $i < $code_num; $i++) {
                                        $dataList[] = array('code' => $codes[$i], "agent_id" => $agent_info['user_id'], "login_name" => $login_name, "prefix" => $prefix, 'add_time' => strtotime($times[0]), 'end_time' => strtotime($times[1]));
                                    };
                                    if (count($dataList) != 0) {
                                        M("InvitationCode")->addAll($dataList);
                                        $return['status'] = 1;
                                    }
                                } else {
                                    $return['status'] = 0;
                                    $return['error'] = "请选择时间范围";
                                }
                            } else {
                                $return['status'] = 0;
                                $return['error'] = "请输入小于200的整数";
                            }
                        } else {
                            $return['status'] = 0;
                            $return['error'] = "前缀已存在，请重新输入！";
                        }
                    }else {
                        $return['status'] = 0;
                        $return['error'] = "前缀不符合条件，请重新输入！";
                    }
            } else {
                $return['status'] = 0;
                $return['error'] = "代理人未启用！";
            }
        } else {
            $return['status'] = 0;
            $return['error'] = "此号码还未成为代理！";
        }
        return $return;
    }

    /*
     * 获取一组邀请码
     *
     * */
    public function getcode($prefix,$num,$codeLen){
        $c=$num;
        $sNumArr1=range(0,9);
        $sNumArr2=range('a','z');
        $sNumArr3=range('A','Z');
        $sPwdArr=array_merge($sNumArr2,$sNumArr1,$sNumArr3);//
        shuffle($sPwdArr);
        $cards=array();
        for($x=0;$x< $c;$x++){
            $tempPwdStr=array();
            for($i=0;$i< $codeLen;$i++){
                $tempPwdStr[]=$sPwdArr[array_rand($sPwdArr)];
            }
            $cards[$x]=$prefix.implode('',$tempPwdStr);
        }
        return $cards;
    }
    /*
     * 查询前缀
     * */
    protected  function checkprefix($prefix)
    {
        $num=M("InvitationCode")->where(array("prefix"=>$prefix))->count();
        if($num==0){
            return true;
        }else{
            return false;
        }
    }
    /*
  * 查询推荐人
  * */
    protected  function checkagent($login_name)
    {

        $agent_info=M("User")->where(array("login_name"=>$login_name))->find();
        //var_dump($agent_info);exit;
        if($agent_info){
            $rs = M("user_area_agent")->where(["user_id" => $agent_info['user_id']])->find();
            if($rs){
                return $agent_info;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }
}